// 实现这个项目的构建任务
const { src, dest, parallel, series, watch } = require('gulp')
const plugins = require('gulp-load-plugins')()
const sass = require('gulp-sass')(require('node-sass'))
const bs = require('browser-sync').create()
const cwd = process.cwd()
let config = {
    build: {
        src: 'src',
        temp: 'temp',
        dist: 'dist',
        public: 'public',
        path: {
            styles: 'assets/styles/*.scss',
            scripts: 'assets/scripts/*.js',
            pages: '*.html',
            images: 'assets/images/**',
            fonts: 'assets/fonts/**',
        }
    },
    data: {}
}
try{
    const loadconfig = require(`${cwd}/pages.config.js`)
    config = Object.assign({}, config, loadconfig)
}catch(error){}
const style = ()=>{
    return src(config.build.path.styles, {base: config.build.src, cwd: config.build.src })
            .pipe(sass({ outputStyle: 'expanded' }))
            .pipe(dest(config.build.temp))
            .pipe(bs.reload({ stream: true }))
}
const script = ()=>{
    return src(config.build.path.scripts, {base: config.build.src, cwd: config.build.src })
            .pipe(plugins.babel({
                presets:[require('@babel/preset-env')]
            }))
            .pipe(dest(config.build.temp))
            .pipe(bs.reload({ stream: true }))
}
const page = ()=>{
    return src(config.build.path.pages, {base: config.build.src, cwd: config.build.src })
            .pipe(plugins.swig({ data: config.data, defaults:{cache: false} }))
            .pipe(dest(config.build.temp))
            .pipe(bs.reload({ stream: true }))
}
const images = ()=>{
    return src(config.build.path.images, {base: config.build.src, cwd: config.build.src })
            .pipe(plugins.imagemin())
            .pipe(dest(config.build.dist))
}
const fonts = ()=>{
    return src(config.build.path.fonts, {base: config.build.src, cwd: config.build.src })
            .pipe(plugins.imagemin())
            .pipe(dest(config.build.dist))
}
const extra = ()=>{
    return src('**', { base: config.build.public, cwd: config.build.public })
            .pipe(dest(config.build.dist))
}
const useref = ()=>{
    return src(config.build.path.pages, { base: config.build.temp, cwd: config.build.temp  })
            .pipe(plugins.useref({ searchPath: [config.build.temp, '.'] }))
            .pipe(plugins.if(/\.js$/, plugins.uglify()))
            .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
            .pipe(plugins.if(/\.html$/, plugins.htmlmin({ collapseWhitespace: true, minifyJS: true, minifyCSS: true })))
            .pipe(dest(config.build.dist))
}
const clean = ()=>{
    return src(config.build.dist + '/*')
            .pipe(plugins.clean())
}
const compile = parallel( style, script, page )
const build = series(clean, parallel(series(compile, useref), extra, images, fonts))
const serve = ()=>{
    watch(config.build.path.styles, { cwd: config.build.src }, style)
    watch(config.build.path.scripts, { cwd: config.build.src }, script)
    watch(config.build.path.pages, { cwd: config.build.src }, page)
    watch([
        config.build.path.images, 
        config.build.path.fonts
    ],{ cwd: config.build.src }, bs.reload)
    watch([
        '**'
    ],{ cwd: config.build.public }, bs.reload)
    bs.init({
        notify: false,
        port: 2021,
        // files: 'dist/**',
        server:{
            baseDir: [config.build.temp, config.build.src, config.build.public],
            routes: {
                '/node_modules' : 'node_modules'
            }
        }
    })
}
const develop = series(build, serve)

module.exports = {
    clean,
    build,
    develop
}